package com.fishsticks.model;

import java.util.ArrayList;

import com.fishsticks.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoryAdapter extends ArrayAdapter<Category> {

	private ArrayList<Category> categories;
	private AlertDialog.Builder cancelDialog;
	
	public CategoryAdapter(Context context, int textViewResourceId, ArrayList<Category> items) {
		super(context, textViewResourceId, items);
		categories = items;
		cancelDialog = new AlertDialog.Builder(getContext(), AlertDialog.THEME_HOLO_LIGHT);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View newView = convertView;
		
		//Creates a new list item from the XML resource
        if (newView == null) {
            LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            newView = vi.inflate(R.layout.category_list_item_row, null);
        }
        
        Category currentCategory = categories.get(position);
        
        if (currentCategory != null) {
                TextView categoryText  = (TextView) newView.findViewById(R.id.category_text_list_view);
                ImageView cancelIcon = (ImageView) newView.findViewById(R.id.cancel_icon_category_list);
                
                if (categoryText != null) 
                	categoryText.setText(currentCategory.getName());
                
                if(cancelIcon != null && (categoryText.getText() != "Personal" && categoryText.getText() != "School" && categoryText.getText() != "Work")) {
                	cancelIcon.setTag(R.id.cancel_button_id, position);
                	
                	cancelIcon.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) { 							
							
							final int cancelButtonTag = (Integer)v.getTag(R.id.cancel_button_id);
							
							cancelDialog.setTitle("Delete");
							cancelDialog.setMessage("Are you sure you want to delete this category?");
							
							cancelDialog.setPositiveButton("Yes", new OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int arg1) {
									DatabaseManager db = new DatabaseManager(getContext());
									db.deleteCategory(categories.get(cancelButtonTag));
									
									setNotifyOnChange(true);
									remove(categories.get(cancelButtonTag));

									db.close();
								}
							});
							
							
							cancelDialog.setNegativeButton("No", new OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int arg1) {
									dialog.dismiss();
								}
							});
							
							cancelDialog.show();
						}
					});
                }
        }
        
        return newView;
	}
	
}
